//
// Created by Jisam on 2024/8/8 11:31.
// solve 4148 生理周期
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int p,e,i,d;
    int idx = 1;
    while(cin >> p >> e >> i >> d && p != -1 && e!= -1 && i!= -1 && d != -1)
    {

        int ans = 0;
        int k = 1;
        int f = 1;
//        if(p == 0 || e == 0 || i == 0){
//            ans = 21252;
//            f = 0;
//        }
//        if(f){

        int pi = (d-p) % 23 ,ei = (d-e) % 28,ii =  (d-i) %33;
//            cout << pi << " "<< ei << " "<< ii << " \n";
        for(k = 1; k <= 21252 ;k ++){
//                cout << (pi + k)%23 << " "<<(ei + k) %28<< " "<<(ii + k ) % 33<< " "<<" \n";
            if((pi + k)%23 == 0 &&  (ei + k) %28  == 0 && (ii   + k ) % 33 == 0){
                ans = k ;
                f = 0;
                break;
            }
        }
        if(f == 1) ans = k;
//        }
        cout << "Case "<<idx<<": the next triple peak occurs in "<< ans <<" days.\n";
        idx ++;
    }
    return 0;
}